﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exem
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(int id, string name, int age, int score)
        {
            InitializeComponent();

            this.id = id;
            textBox1.Text = name;
            textBox2.Text = age.ToString();
            textBox3.Text = score.ToString();
        }

        private int id;

        private void button1_Click(object sender, EventArgs e)
        {
            var nameStr = textBox1.Text.Trim();
            var ageStr = textBox2.Text;
            var scoreStr = textBox3.Text;

            var name = nameStr.Length > 0 ? nameStr : "赖";
            var age = ageStr.Length > 0 ? int.Parse(ageStr) : 18;
            var score = scoreStr.Length > 0 ? int.Parse(scoreStr) : 0;

            if (name == "赖")
            {
                MessageBox.Show("学生姓名不能为空！", "提示");
            }
            else
            {
                if (this.id > 0)
                {
                    var sql = string.Format("update Students set  Name='{0}',age={1},Score={2} where id ={3}", name, age, score, this.id);
                    var dt = DbHelper.AddOrUpdateOrDelete(sql);
                    MessageBox.Show("更新成功");

                }
                else
                {
                    var sql = string.Format("insert into Students(Name, Age, Score) values('{0}', {1}, {2})", name, age, score);
                    DbHelper.AddOrUpdateOrDelete(sql);
                    MessageBox.Show("添加成功", "提示");

                }
                this.DialogResult = DialogResult.Yes;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
